<template>
  <div>
    <el-input v-model="params.username" style="width: 200px" placeholder="请输入姓名"></el-input>
    <el-input v-model="params.phone" style="width: 200px; margin-left: 5px" placeholder="请输入电话"></el-input>
    <el-button type="warning" style="margin-left: 10px" @click="findBySearch()">查询</el-button>
    <el-button type="warning" @click="reset()">清空</el-button>
    <el-button type="primary" style="margin-left: 10px" @click="add()">新增</el-button>

    <el-table :data="tableData" style="width: 100%; margin: 15px 0px">
      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="employeeNumber" label="工号"></el-table-column>
      <el-table-column prop="department" label="部门"></el-table-column>
      <el-table-column prop="phone" label="联系方式"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="params.current" :page-sizes="[5, 10, 15, 20]" :page-size="params.size"
        layout="total, sizes, prev, pager, next" :total="total">
      </el-pagination>
    </div>

    <!--    // 新增更新-->
    <el-dialog title="请填写信息" v-model="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="auto">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="employeeNumber">
          <el-input v-model="form.employeeNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="operate == 1">
          <el-input v-model="form.password" placeholder="请输入密码" show-password ></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="form.department">
            <el-option v-for="item in departmentOpt" :label="item.label" :value="item.value"
              :key="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限" prop="remark">
          <el-select v-model="form.remark">
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      operate: 1,
      params: {
        name: '',
        phone: '',
        current: 1,
        size: 5,
        role: '',
        employeeNumber: ''
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {
        email: ''
      },
      departmentOpt: [
        {
          label: '企业管理部',
          value: '企业管理部'
        },
        {
          label: '设备部',
          value: '设备部'
        },
        {
          label: '生产部',
          value: '生产部'
        },
        {
          label: '采购运营中心',
          value: '采购运营中心'
        },
        {
          label: '质量控制部',
          value: '质量控制部'
        },
        {
          label: '工程研发部',
          value: '工程研发部'
        },
        {
          label: '行政部',
          value: '行政部'
        },
        {
          label: '安全环保部',
          value: '安全环保部'
        },
        {
          label: '财务部',
          value: '财务部'
        },
        {
          label: '人力资源部',
          value: '人力资源部'
        },
        {
          label: '综合部',
          value: '综合部'
        },
      ],
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: false, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        department: [
          { required: false, message: '请选择部门', trigger: 'change' },
        ],
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        role: [
          { required: true, message: '请选择用户身份', trigger: 'change' },
        ],
        remark: [
          { required: true, message: '请选择用户权限', trigger: 'change' },
        ],
      }
    }
  },
  created() {
    this.findBySearch()
  },
  methods: {
    // 模糊查询
    findBySearch() {
      request.get(`/user/page/${this.params.current}/${this.params.size}`, { params: this.params }).then(result => {
        this.tableData = result.records
        this.total = result.total

      })
    },
    reset() {
      this.params = {
        current: 1,
        size: 5,
        username: '',
        phone: ''
      }
      this.findBySearch()
    },
    handleSizeChange(size) {
      this.params.size = size
      this.findBySearch()
    },
    handleCurrentChange(current) {
      this.params.current = current
      this.findBySearch()
    },
    add() {
      this.operate = 1
      this.form = {}
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.operate = 2
      this.form = obj;
      console.log(this.form);
      this.dialogFormVisible = true;
    },
    submit() {
      request.post("/user/save", this.form).then(res => {
        if (res.code == '200') {
          this.$message.success("操作成功")
          this.dialogFormVisible = false;
          this.findBySearch();
        } else {
          this.$message({
            message: res.data,
            type: 'error'
          });
        }
      })
    },
    del(id) {
      request.delete("/user/delete/" + id).then(res => {
        if (res.code == '200') {
          this.$message.success("删除成功")
          this.findBySearch();
        } else {
          this.$message({
            message: res.data,
            type: 'success'
          });
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submit()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.el-pagination {
  float: right;
}
</style>